package com.snif.web.attach.core;

import com.snif.web.attach.core.server.ConfigurablePortHttpServer;
import com.snif.web.attach.core.utils.ClassTik;

import java.io.IOException;
import java.util.Map;

/**
 * WebStarter
 *
 * @author Yakir
 */
public class WebStarter {
    private static ConfigurablePortHttpServer httpServer;

    public static void startApp(){
        startApp("");
    }
    public static void startApp(String agentArgs){
        try {
            // 解析Agent参数
            Map<String, String> params = ClassTik.parseAgentArgs(agentArgs);
            // 解析Agent参数（格式：key1=value1,key2=value2 或 key1=value1;key2=value2）
            // 获取指定的端口（参数名：port）
            Integer specifiedPort = null;
            if (params.containsKey("port")) {
                try {
                    specifiedPort = Integer.parseInt(params.get("port"));
                }
                catch (NumberFormatException e) {
                    System.out.println("无效的端口参数：" + params.get("port") + "，将使用动态端口");
                }
            }

            // 启动HTTP服务（传入指定的端口）
            httpServer = new ConfigurablePortHttpServer();
            httpServer.start(specifiedPort);
        } catch (IOException e) {
            System.out.println("探针启动失败: " + e.getMessage());
            e.printStackTrace();
        }


    }
}
